'''
Created on 2011-10-30

@author: tony.li.fly@gmail.com
'''
from xyh.oa.models import Currency
from django.http import HttpResponse
from django.utils import simplejson as json
from xyh.oa.utils.utils import Encoder


def listall(request):
    result = []
    for c in Currency.objects.order_by('code').all():
        result.append({'id':c.id, 'code':c.code, 'rate':c.rate})
    return HttpResponse(json.dumps(result, cls=Encoder))


def delCurrency(request, currency_id):
    currency = Currency.objects.get(pk=currency_id)
    currency.delete()
    return HttpResponse()
    
def addCurrency(request):
    currency = Currency(code=request.POST['code'], rate=request.POST['rate'])
    currency.save()
    return HttpResponse()

def updateCurrency(request):
    currency = Currency.objects.get(pk=request.POST['id'])
    currency.code = request.POST['code']
    currency.rate = request.POST['rate']
    currency.save()
    return HttpResponse()
